package com.github.wui.comm;

import static android.graphics.Color.BLACK;
import static android.graphics.Color.WHITE;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;


import com.github.wui.R;
import com.github.wui.circle.CircleLinearLayout;

/**
 * 项目名称: TreasureChest
 * 包名 :  com.gitcode.treasurechest.customize
 *
 * @author Mr.xie
 * @Date 2025/8/7
 */
public class LabelSwitchView extends CircleLinearLayout {

    public interface ILabelSwitchViewListener {
        /**
         * 当某个 label 被选中时回调
         *
         * @param index 选中的位置
         * @param value 对应的文字
         */
        void onLabelClick(int index, String value);
    }

    /* ---------- 属性 ---------- */
    private CharSequence[] labels = null;
    private int checkedIndex = 0;                 // 当前高亮位置
    private ILabelSwitchViewListener listener;

    /* ---------- 颜色可配置 ---------- */
    private static  int color_default_bg;
    private static final String COLOR_DEFAULT_SELECTED = "#FFFFFF";
    private float innerRound = 0;

    private int selected_background_color = WHITE;

    private int selected_text_color = Color.BLACK;

    private int unselected_text_color = Color.BLACK;

    private int vSpan = 10;
    private int hSpan = 10;

    /* ---------- 构造 ---------- */
    public LabelSwitchView(Context context) {
        this(context, null);
    }

    public LabelSwitchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LabelSwitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.HORIZONTAL);

        // 读取自定义属性
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LabelSwitchView);
        labels = ta.getTextArray(R.styleable.LabelSwitchView_labels);
        selected_background_color = ta.getColor(R.styleable.LabelSwitchView_selected_background_color, WHITE);
        selected_text_color = ta.getColor(R.styleable.LabelSwitchView_selected_text_color, BLACK);
        unselected_text_color = ta.getColor(R.styleable.LabelSwitchView_unselected_text_color, BLACK);
        innerRound = ta.getDimension(R.styleable.LabelSwitchView_inner_round, 0);
        color_default_bg = ta.getColor(R.styleable.LabelSwitchView_unselected_background_color, Color.parseColor("#f6f6f6"));
        ta.recycle();

        // 创建子 TextView
        if (labels != null) {
            for (int i = 0; i < labels.length; i++) {
                TextView tv = createLabelView(context, labels[i]);

                final int index = i;
                GradientDrawable gradientDrawable = new GradientDrawable();
                gradientDrawable.setBounds((int) innerRound, (int) innerRound, (int) innerRound, (int) innerRound);
                gradientDrawable.setCornerRadius(innerRound);
                if (index == checkedIndex) {
                    gradientDrawable.setColor(selected_background_color);
                    tv.setBackground(gradientDrawable);
                    tv.setTextColor(selected_text_color);
                } else {
                    gradientDrawable.setColor(color_default_bg);
                    tv.setBackground(gradientDrawable);
                    tv.setTextColor(unselected_text_color);
                }
                tv.setOnClickListener(v -> setCheckedIndex(index));
                addView(tv);
            }
        }

        // 初始化默认选中
        setCheckedIndex(checkedIndex);
    }


    /* ---------- 内部方法 ---------- */
    private TextView createLabelView(Context context, CharSequence text) {
        TextView tv = new TextView(context);
        tv.setTextSize(12f);
        tv.setPadding(dp2px(12), dp2px(6), dp2px(12), dp2px(6));
        tv.setText(text);
        tv.setTextColor(Color.BLACK);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(hSpan,vSpan,hSpan,vSpan);
        tv.setLayoutParams(params);
        return tv;
    }

    private int dp2px(float dp) {
        return (int) (getResources().getDisplayMetrics().density * dp + 0.5f);
    }

    private void refreshUI() {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            TextView child = (TextView) getChildAt(i);
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setBounds((int) innerRound, (int) innerRound, (int) innerRound, (int) innerRound);
            gradientDrawable.setCornerRadius(innerRound);
            if (i == checkedIndex) {
                gradientDrawable.setColor(selected_background_color);
                child.setBackground(gradientDrawable);
                child.setTextColor(selected_text_color);
            } else {
                gradientDrawable.setColor(color_default_bg);
                child.setBackground(gradientDrawable);
                child.setTextColor(unselected_text_color);
            }
        }
    }

    /* ---------- 公开 API ---------- */

    /**
     * 主动设置选中的 label
     *
     * @param newIndex 新的选中位置
     */
    public void setCheckedIndex(int newIndex) {
        if (newIndex < 0 || newIndex > getChildCount()) {
            return;
        }

        checkedIndex = newIndex;
        refreshUI();

        if (listener != null) {
            TextView checkedView = (TextView) getChildAt(checkedIndex);
            listener.onLabelClick(checkedIndex, checkedView.getText().toString());
        }
    }

    public int getCheckedIndex() {
        return checkedIndex;
    }

    public void setListener(ILabelSwitchViewListener listener) {
        this.listener = listener;
        setCheckedIndex(0);
    }

}